package com.tfswue.group.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.tfswue.group.entity.Review;
import com.tfswue.group.service.ReviewService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.parameters.RequestBody;
import io.swagger.v3.oas.annotations.responses.ApiResponse;
import io.swagger.v3.oas.annotations.tags.Tag;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.annotation.CacheEvict;
import org.springframework.cache.annotation.CachePut;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@Slf4j
@RestController
@RequestMapping("/api/reviews")
@Tag(name = "评价接口", description = "符合RESTful风格的评价管理接口")
public class ReviewController {

    @Autowired
    private ReviewService reviewService;

    public ReviewController() {

        log.info("ReviewController 已初始化");
    }

    /**
     * 添加评价
     *
     * @param review 请求体中的评价信息
     * @return 成功与否
     */
    @Operation(summary = "添加一个评价", description = "添加商品评价")
    @ApiResponse(responseCode = "200", description = "成功添加评价")
    @PostMapping
    public boolean addReview(@RequestBody Review review) {
        return reviewService.save(review);
    }

    /**
     * 查询指定ID的评价
     *
     * @param id 评价ID
     * @return 评价实体
     */
    @Operation(summary = "查询评价", description = "根据ID查询评价")
    @ApiResponse(responseCode = "200", description = "成功获取评价")
    @Cacheable(value = "dataCache", key = "#id")
    @GetMapping("/{id}")
    public Review getReviewById(@PathVariable Integer id) {
        log.info("查询评价ID: {}", id);
        return reviewService.getById(id);
    }

    /**
     * 更新评价信息
     *
     * @param review 新的评价信息
     * @return 成功与否
     */
    @Operation(summary = "更新评价", description = "更新评价信息")
    @ApiResponse(responseCode = "200", description = "成功更新评价")
    @CachePut(value = "dataCache", key = "#review.id")
    @PutMapping
    public boolean updateReview(@RequestBody Review review) {
        return reviewService.updateById(review);
    }

    /**
     * 删除指定ID的评价
     *
     * @param id 评价ID
     * @return 成功与否
     */
    @Operation(summary = "删除评价", description = "根据ID删除评价")
    @ApiResponse(responseCode = "200", description = "成功删除评价")
    @CacheEvict(value = "dataCache", key = "#id")
    @DeleteMapping("/{id}")
    public boolean deleteReview(@PathVariable Integer id) {
        return reviewService.removeById(id);
    }

    /**
     * 分页查询评价列表
     *
     * @param page 当前页码
     * @param size 页面大小
     * @return 当前页的评价列表
     */
    @Operation(summary = "分页查询评价", description = "分页获取评价列表")
    @ApiResponse(responseCode = "200", description = "成功分页获取评价")
    @GetMapping("/page/{page}/{size}")
    public List<Review> getReviewsByPage(@PathVariable Integer page, @PathVariable Integer size) {
        Page<Review> reviewPage = new Page<>(page, size);
        return reviewService.page(reviewPage).getRecords();
    }
}
